import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";

/**
 * 轨道相机
 */
export default class OrbitCameraComponent {
    private static instance: OrbitCameraComponent = new OrbitCameraComponent();
    /**
     * 轨道相机
     */
    public static get Default(): OrbitCameraComponent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);
    }

    /**
     * 设置视角Id
     * @param id 视角Id
     */
    public async ApplyConfigAsync(id: number): Promise<boolean> {
        const response = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.OrbitCameraApplyConfigRequest(id));
        return response.Error == 0;
    }

    /**
     * 更新视角
     * @param position 位置
     * @param horizontal 水平角度
     * @param vertical 垂直角度
     * @param distance 距离
     * @param minDistance 最小距离
     * @param maxDistance 最大距离
     * @returns 操作结果
     */
    public async UpdateAsync(position: Array<number>, horizontal: number, vertical: number, distance: number, minDistance: number, maxDistance: number): Promise<boolean> {
        const response = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.OrbitCameraUpdateRequest(position, horizontal, vertical, distance, minDistance, maxDistance));
        return response.Error == 0;
    }

    public async UpdateV2Async(path:string, horizontal: number, vertical: number, distance: number, minDistance: number, maxDistance: number,horizontalSpeed:number,verticalSpeed:number,distanceSpeed:number): Promise<boolean> {
        const response = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.OrbitCameraUpdateV2Request(path, horizontal, vertical, distance, minDistance, maxDistance,89,-89,horizontalSpeed,verticalSpeed,distanceSpeed*100,360,-360));
        return response.Error == 0;
    }
}